﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RGPWEB.admin
{
    public partial class Main : System.Web.UI.Page
    {
        private DataTable ModuleTable;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!SessionBox.CheckUserSession())
            {
                Response.Redirect("~/login.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    try
                    {
                        InitMenu();
                    }
                    catch
                    {
                        MenuTreeView.Nodes.Clear();
                    }
                }
            }
        }

        /// <summary>
        /// 初始化菜单
        /// </summary>
        protected void InitMenu()
        {
            RGP.BLL.RGP_Modules bll = new RGP.BLL.RGP_Modules();
            TreeNode node = new TreeNode();
            ModuleTable = bll.GetModuleTypeList("").Tables[0];  //取得所有数据得到DataTable 
            LoadNode(MenuTreeView.Nodes, "0");
            MenuTreeView.CollapseImageToolTip = "";
            MenuTreeView.ExpandImageToolTip = "";
        }

        /// <summary>
        /// 递归菜单（模块列表）
        /// </summary>
        /// <param name="node">子菜单</param>
        /// <param name="MtID">子菜单上级ID</param>
        protected void LoadNode(TreeNodeCollection node, string MtID)
        {
            DataView dvList = new DataView(ModuleTable);
            dvList.RowFilter = "ModuleTypeSuperiorID=" + MtID;  //过滤父节点 
            TreeNode nodeTemp;
            foreach (DataRowView dv in dvList)
            {
                nodeTemp = new TreeNode();
                nodeTemp.Value = dv["ModuleTypeID"].ToString() + ",0";
                nodeTemp.Text = dv["ModuleTypeName"].ToString();  //节点名称                 
                node.Add(nodeTemp);  //加入节点 
                nodeTemp.SelectAction = TreeNodeSelectAction.Expand;
                nodeTemp.Expanded = false;

                this.LoadNode(nodeTemp.ChildNodes, nodeTemp.Value.Split(',')[0]);  //递归 

                //增加子模块分类
                RGP.BLL.RGP_Modules bll = new RGP.BLL.RGP_Modules();
                DataSet Module = bll.GetModuleList("ModuleTypeID=" + dv["ModuleTypeID"].ToString());
                int i = 0;
                foreach (DataRow child_dr in Module.Tables[0].Rows)
                {
                    if ((SessionBox.GetUserSession().IsLimit == true
                            || (child_dr["ModuleDisabled"].ToString().ToLower() == "true"
                            && UserHandle.ValidationModule(int.Parse(child_dr["ModuleID"].ToString()), RGP_Tag.Browse)))
                            && child_dr["IsMenu"].ToString().ToLower() == "true")
                    {
                        TreeNode ChildNode = new TreeNode(child_dr["ModuleName"].ToString());
                        ChildNode.Value = child_dr["ModuleID"].ToString() + ",1";
                        ChildNode.Expanded = false;
                        ChildNode.NavigateUrl = child_dr["ModuleURL"].ToString();  //节点链接地址 
                        ChildNode.Target = "modulePanel";
                        nodeTemp.ChildNodes.Add(ChildNode);
                        i++;
                    }
                }

                //删除不必要的模块分类节点。
                if (i == 0)
                {
                    MenuTreeView.Nodes.Remove(nodeTemp);
                }
            }
        }
    }
}
